# to run the test, one needs to (apt-get) install:
# dejagnu tcl tcllib valgrind

all:  clean rest valgrind.res
test: clean dunetest qtest
rest: reftest lus2lic.time
jenkins: clean qtest rest


ifndef TEST_MACHINE
 TEST_MACHINE=eval
endif
# This fixes sporadic memory errors I get when running tests
OCAMLRUNPARAM=s=1M,i=32M,o=150

dunetest:
	dune runtest

copy_extern:
	cp -f extern_files/* .

testdir=$(shell pwd)

remote-runtest:
#	runtest --all --tool lus2lic --target_board=$(testdir)/board_$(TEST_MACHINE).exp  || true
	$(TEST_MACHINE) "cd  $(testdir) ; export OCAMLRUNPARAM=o=200 ; runtest --all --tool lus2lic"  || true
	cd $(testdir)

LOG=lus2lic0.log lus2lic1.1.log lus2lic1.2.log lus2lic1.3.log lus2lic1.4.log  \
		 lus2lic2.1.log lus2lic2.2.log lus2lic2.3.log lus2lic2.4.log
SUM=lus2lic0.sum lus2lic1.1.sum lus2lic1.2.sum lus2lic1.4.sum lus2lic1.3.sum   \
		 lus2lic2.1.sum lus2lic2.2.sum lus2lic2.4.sum lus2lic2.3.sum

lus2lic.sum:
	uname -a > lus2lic.sum
	head -n -5 $(SUM) | sed -e 's/[0-9][0-9][0-9][0-9][0-9]//' | sed -e 's/[0-9][0-9][0-9][0-9]//' >> lus2lic.sum
	tail -n 5  $(SUM)  >>  lus2lic.sum
	echo -n "===============================\n# Total number of failures: " >>  lus2lic.sum
	cat lus2lic*.log | grep "FAIL:" | grep -v "XFAIL" | wc -l >>  lus2lic.sum

.PHONY:lus2lic.diff lus2lic.time lus2lic.sum
lus2lic.diff:
	cat lus2lic.log | grep  -v "Process " | grep -v "Killing process " | \
		grep -v "simec_trap " | grep -v " Version "  > lus2lic2.log
	mv lus2lic2.log lus2lic.log
	diff -U 1 -I "Test Run By" -I "testcase \./lus2lic" -I "runtest completed at" \
	 -I "This is Lurette Version "  \
	lus2lic.log.ref lus2lic.log  > lus2lic.diff || \
	(cat lus2lic.diff; echo "make utest if it is fine"; exit 1)

time:lus2lic.time
lus2lic.time:lus2lic.sum
	grep "testcase \./lus2lic" lus2lic*.log > lus2lic.time
	echo "* Ref time: " >> lus2lic.time
	cat lus2lic.ref_time >> lus2lic.time || true
	echo "* Quick time (-j 4):" >> lus2lic.time
	cat lus2lic.quick_time >> lus2lic.time || true
	cat lus2lic.time >> lus2lic.sum
	cat lus2lic.time
.PHONY:lus2lic.time

utest:
	cp lus2lic.log lus2lic.log.ref

# check if valgrind (called in compare_exec_and_2c) has generated errors
.PHONY:valgrind.res
valgrind.res:
	grep -n ERROR tmp1/*.log | grep -v ' 0 errors' > $@;  [ ! -s $@ ]

# reftest is slower than qtest (less proc are used), but less sensible
# to the load of the machine (which is better wrt time measurements)
reftest:
	rm -f $(LOG)
	$(TEST_MACHINE) "cd $(testdir); make tmpdirs"
	time -o lus2lic.ref_time make $(LOG)
	make lus2lic.time
ifndef CORE_NB
	CORE_NB=4
endif


qtest:
	rm -f $(LOG)
	$(TEST_MACHINE) "cd $(testdir); make tmpdirs"
	time -o lus2lic.quick_time make -j $(CORE_NB) $(LOG)
	cat lus2lic.quick_time
	make lus2lic.time

ltmpdirs:
	[ -d /tmp/lus2lic-test ] || mkdir /tmp/lus2lic-test
	[ -d /tmp/lus2lic-test-tmp1 ] || mkdir /tmp/lus2lic-test-tmp1
	[ -d /tmp/lus2lic-test-tmp2 ] || mkdir /tmp/lus2lic-test-tmp2

tmpdirs: ltmpdirs
	[ -d tmp ] || mkdir tmp
	[ -d tmp1 ] || mkdir tmp1
	[ -d tmp2 ] || mkdir tmp2
	rm -rf tmp/*
	rm -rf tmp1/*
	rm -rf tmp2/*
	cp -f --preserve=links $(testdir)/* tmp/ || true
	cp -f --preserve=links $(testdir)/* tmp1/ || true
	cp -f --preserve=links $(testdir)/* tmp2/ || true


unexp:
	grep unexp lus2lic.sum


DIR=lus2lic.tests/

lus2lic0.log:
	$(TEST_MACHINE) "cd $(testdir); runtest --tool lus2lic0 $(DIR)test0.exp" || true

lus2lic1.1.log:
	$(TEST_MACHINE) "cd $(testdir); runtest --tool lus2lic1.1 $(DIR)test1_1.exp" || true
lus2lic1.2.log:
	$(TEST_MACHINE) "cd $(testdir); runtest --tool lus2lic1.2 $(DIR)test1_2.exp" || true
lus2lic1.3.log:
	$(TEST_MACHINE) "cd $(testdir); runtest --tool lus2lic1.3 $(DIR)test1_3.exp" || true
lus2lic1.4.log:
	$(TEST_MACHINE) "cd $(testdir); runtest --tool lus2lic1.4 $(DIR)test1_4.exp" || true

lus2lic2.1.log:
	$(TEST_MACHINE) "cd $(testdir); runtest --tool lus2lic2.1 $(DIR)test2_1.exp" || true
lus2lic2.2.log:
	$(TEST_MACHINE) "cd $(testdir); runtest --tool lus2lic2.2 $(DIR)test2_2.exp" || true
lus2lic2.3.log:
	$(TEST_MACHINE) "cd $(testdir); runtest --tool lus2lic2.3 $(DIR)test2_3.exp" || true
lus2lic2.4.log:
	$(TEST_MACHINE) "cd $(testdir); runtest --tool lus2lic2.4 $(DIR)test2_4.exp" || true



kcg.log:
	$(TEST_MACHINE) "cd $(testdir); runtest --tool lus2lic-kcg kcg.exp" || true

kcg: kcg.log

progression:
	$(TEST_MACHINE) "cd $(testdir); runtest --tool lus2lic progression.exp" || true



local_clean: lclean tmpdirs
clean:
	cd $(testdir); make local_clean; rm -f valgrind.res
lclean:
	rm -f *.ec *.lus *.lut *.cov *.gp *.rif *.out *.cov *.c *.h *.log *.sum *time
	rm -rf Data

# not gitted stuff
-include ./Makefile.local
